package lib.bone.admin.loader;

import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.exception.BusinessException;
import lib.bone.flow.constant.AssignConst;
import lib.bone.flow.service.FlowTaskAssignService;
import lib.bone.flow.domain.dto.FlowParam;
import lib.bone.flow.domain.entity.FlowTaskAssign;
import lib.bone.flow.domain.entity.FlowWork;
import lib.bone.flow.domain.model.BaseModel;
import lib.bone.flow.module.loader.FlowNodeUserLoader;
import lib.bone.system.domain.entity.SysUser;
import lib.bone.system.service.SysDeptService;
import lib.bone.system.service.SysUserRoleService;
import lib.bone.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 操作用户获取
 * @author: kindear
 */
@Slf4j
@Service
public class FlowNodeUserLoaderImpl implements FlowNodeUserLoader {
    @Autowired
    SysUserRoleService sysUserRoleService;
    @Autowired
    SysDeptService sysDeptService;
    @Autowired
    SysUserService sysUserService;
    // 注入分配函数
    @Autowired
    FlowTaskAssignService flowTaskAssignService;

    @Override
    public List<String> loadUsers(FlowWork flowWork, BaseModel baseModel, FlowParam param) {
        // 流程定义编码
        String flowDefineCode = flowWork.getFlowDefineCode();
        //
        List<FlowTaskAssign> assigns = flowTaskAssignService.getLinkAssign(flowDefineCode, baseModel.getId());
        // 5-4 新增 xy-check 根据用户进行区分
        if(baseModel.getId().equals("xy-check")){
            // 获取发起人信息
            Long userId = Long.parseLong(flowWork.getInitiator());
            //log.info("发起人用户ID:{}",userId);
            Optional<SysUser> userOp = sysUserService.getById(userId);
            if (!userOp.isPresent()) throw new BusinessException(ResponseCodeConst.SYSTEM_ERROR,"用户信息查找失败");
            SysUser user = userOp.get();
            Long deptId = user.getDeptId();
            //log.info("发起人学院ID:{}",deptId);
            // 获取对应学院预审核
            assigns = assigns.stream().filter(e -> e.getIsCollege().equals(1) && e.getDeptId().equals(deptId))
                    .collect(Collectors.toList());

        }

        // 根据查询的内容获取用户ID
        Set<String> userIds = new HashSet<>();
        assigns.forEach(flowTaskAssign -> {
            if (flowTaskAssign.getAssignType().equals(AssignConst.DEPT)){
                // 如果是按照部门授权
                List<SysUser> deptUsers = sysUserService.findByDept(Long.parseLong(flowTaskAssign.getAssignId()));
                deptUsers.forEach(sysUser -> {
                    userIds.add(sysUser.getId().toString());
                });
            }else if (flowTaskAssign.getAssignType().equals(AssignConst.ROLE)){
                // 如果是按照角色授权
                List<SysUser> roleUsers = sysUserRoleService.getRoleUsers(Long.parseLong(flowTaskAssign.getAssignId()));
                roleUsers.forEach(sysUser -> {
                    userIds.add(sysUser.getId().toString());
                });
            }else if (flowTaskAssign.getAssignType().equals(AssignConst.USER)){
                userIds.add(flowTaskAssign.getAssignId());
            }
        });
        log.info("授权用户ID列表为:"+userIds.toString());
        return new ArrayList<>(userIds);
    }
}
